/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SignerDialog.java
 *
 * Created on Jan 29, 2012, 5:54:58 PM
 */
package it.j4ops.gui.dialog;

import it.j4ops.util.DNParser;
import it.j4ops.verify.bean.SignerInfo;
import java.awt.Dialog;
import java.awt.Window;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author zanutto
 */
public class SignerDialog extends javax.swing.JDialog {
    private SignerInfoTableModel signerInfoTableModel = new SignerInfoTableModel();
    private List<SignerInfo> lstSigners;
    private SignerInfo selectedSignerInfo;

    /** Creates new form SignerDialog */
    public SignerDialog(Window owner, Dialog.ModalityType modalityType, final List<SignerInfo> lstSigners) {
        super(owner, modalityType);
        this.lstSigners = lstSigners;       
        initComponents();
        signerInfoTableModel.removeAll();
        for (SignerInfo signerInfo : lstSigners) {
            signerInfoTableModel.addSignerInfo(signerInfo);
        }
    }
    
    public SignerInfo getSelectedSignerInfo () {
        return selectedSignerInfo;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbutCancel = new javax.swing.JButton();
        jbutOk = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtabSigners = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jbutCancel.setText("Cancel");
        jbutCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbutCancelActionPerformed(evt);
            }
        });

        jbutOk.setText("OK");
        jbutOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbutOkActionPerformed(evt);
            }
        });

        jtabSigners.setModel(signerInfoTableModel);
        jScrollPane1.setViewportView(jtabSigners);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbutOk, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbutCancel))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbutCancel)
                    .addComponent(jbutOk))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbutOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbutOkActionPerformed
        if (jtabSigners.getSelectedRow() >= 0) {
            selectedSignerInfo = lstSigners.get(jtabSigners.getSelectedRow());
            dispose();   
        }
    }//GEN-LAST:event_jbutOkActionPerformed

    private void jbutCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbutCancelActionPerformed
        selectedSignerInfo = null;
        dispose();
    }//GEN-LAST:event_jbutCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SignerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SignerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SignerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SignerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                List<SignerInfo> lstSigners = new ArrayList<SignerInfo>();
                SignerDialog dialog = new SignerDialog(new javax.swing.JFrame(), Dialog.ModalityType.APPLICATION_MODAL, lstSigners);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbutCancel;
    private javax.swing.JButton jbutOk;
    private javax.swing.JTable jtabSigners;
    // End of variables declaration//GEN-END:variables
}


class SignerInfoTableModel extends DefaultTableModel {   
    
    public SignerInfoTableModel () {
        setDataVector(new Object[0][0], new String[]{"Level", "Owner", "Organization", "Issuer", "DateSign", "SignType", "isCounterSign"});
    }        
    
    public void removeAll () {
        for (int index = 0; index < getRowCount(); index++) {
            removeRow(index);      
        }
        getDataVector().clear();        
    }
    
    public void addSignerInfo (SignerInfo signerInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss");
        insertRow(getRowCount(), new String[]{"" + signerInfo.getLevel(),
                                              signerInfo.getAuthor(),
                                              DNParser.parse(signerInfo.getX509Cert().getSubjectDN().toString(), "O"),
                                              DNParser.parse(signerInfo.getX509Cert().getIssuerDN().toString(), "CN"),
                                              sdf.format(signerInfo.getDateSign()),
                                              "" + signerInfo.getSignType(),
                                              "" + signerInfo.isCounterSignature()});            
    }    
    
    @Override
    public boolean isCellEditable(int row, int column) {
       return false;
    }    
                         
}